package themis.flink.riskengine;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StrRuleParser implements IRuleParser {
    public List<Rule> parseDSL(String str) {
        String[] parseArr = parse(str);
        String logic = parseArr[0];
        String measure = parseArr[1].split("#")[0];
        String dimension = parseArr[1].split("#")[0];
        if ((parseArr[1].split("#")).length > 1)
            dimension = parseArr[1].split("#")[1];
        Integer interval = Integer.valueOf(parseArr[2]);
        Rule rule = new Rule(logic, dimension, measure, interval);
        ArrayList<Rule> rules = new ArrayList<>();
        rules.add(rule);
        return rules;
    }

    private static String[] parse(String dsl) {
        return (String[])((List)Arrays.<String>stream(dsl.split("[(,)]")).map(String::trim)
                .collect(Collectors.toList())).toArray((Object[])new String[0]);
    }

    public static void main(String[] args) {
        StrRuleParser strRuleParser = new StrRuleParser();
        List<Rule> rules = strRuleParser.parseDSL("sum(amount#rcv_account.history,100000)");
        System.out.println(rules);
    }
}
